
(function ($) {
    $.fn.FeedEk = function (opt,callGetTitle) {
        var def = $.extend({
            FeedUrl: "http://rss.cnn.com/rss/edition.rss",
            MaxCount: 5,
            ShowHead: true,
            ShowDesc: true,
            ShowPubDate: false,
            DescCharacterLimit: 120,
            TitleLinkTarget: "_blank"
        }, opt);

        var id = $(this).attr("id");
        var i;
        var rElmImg = /<img[^>]*?\/?>/gi,   //get all img element
            rElmAttr1 = /(width|height){1}[^\"]*?\"\s?0[^\"]*?\"/gi, //0尺寸的img
            rElmAttr2 = /(align|width|height|style){1}[^\"]*?\"[^(0|\")?][^\"]*?\"/gi,  ////align、非0的尺寸
            rElmHtml = /(<[^>]*?>|\s\s)/gi; //HTML element
        $("#" + id).empty().append('<img src="image/loader.gif" />');
        $.ajax({
            url: "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=" + def.MaxCount + "&output=json&q=" + encodeURIComponent(def.FeedUrl) + "&hl=en&callback=?",
            dataType: "json",
            error: function (data) {
                alert('Error:The feed can not be displayed.' + data);
            },
            success: function (data) {
                $("#" + id).empty();
                if (data.responseStatus == 200) {
                    var oFeed = data.responseData.feed;
                    var sHtml = "";
                    var aHtml = [];
                    var bShowPubDate = def.ShowPubDate;
                    var bShowDesc = def.ShowDesc;
                    var iLimit = def.DescCharacterLimit;
                    if (def.ShowHead) {
                        if (oFeed.entries[0]) {
                            var sDate = func1.ConvToLocalTime(oFeed.entries[0].publishedDate);
                        }
                        aHtml.push('<div class="listHead">最后更新：' + sDate + '<br/>');
                        aHtml.push('订阅链接：' + oFeed.feedUrl + '&nbsp;&nbsp;<abbr title="添加到您的列表" unselectable="on" class="glyphicon glyphicon-plus pointer"></abbr><br/>');
                        aHtml.push('标题：<a href="' + oFeed.link + '" target="_blank">' + oFeed.title + '</a><br/>');
                        aHtml.push("简介：" + oFeed.description + '<br/></div>');
                    }
                    $.each(oFeed.entries, function (e, item) {
                        aHtml.push('<div class="itemOne">');
                        aHtml.push('<div unselectable="on" class="itemIcon glyphicon glyphicon-chevron-down"></div>');
                        aHtml.push('<div class="itemTitle"><li><a href="' + item.link + '" target="' + def.TitleLinkTarget + '" >' + item.title + "</a></li></div>");
                        if (bShowPubDate) {
                            var sDate = func1.ConvToLocalTime(item.publishedDate);
                            aHtml.push('<div class="itemDate">' + sDate + "</div>");
                        }
                        if (bShowDesc) {
                            var sContent = item.content;
                            var aImg,sImg;                           
                            aImg = sContent.match(rElmImg);  //get all img element
                            sContent = sContent.replace(rElmHtml, "");   //delete HTML element
                            aHtml.push('<div class="itemContent">');
                            if (iLimit > 0 && sContent.length > iLimit) {
                                aHtml.push(sContent.substr(0, iLimit));
                                aHtml.push('<span class="iHide">&nbsp;');
                                if (sContent.length > iLimit * 3) {
                                    aHtml.push(sContent.substr(iLimit, iLimit * 2));
                                } else {
                                    aHtml.push(sContent.substr(iLimit));
                                }
                                aHtml.push('</span>...');
                            } else {
                                aHtml.push(sContent);
                            }
                            aHtml.push('</div>');
                            if (aImg != null) {
                                aHtml.push('<div class="iHide itemImg">');
                                for (var i = 0; (i < aImg.length) && (i<3); i++) {
                                    if (aImg[i].match(rElmAttr1) == null) {                   //如果有0尺寸的就不显示
                                        sImg = aImg[i].replace(rElmAttr2, "");    //去除align、非0的尺寸
                                        sImg = sImg.replace(/src/gi, "delayLoad");
                                        aHtml.push(sImg);
                                    }
                                }
                                aHtml.push("</div>");
                            }
                        }
                        aHtml.push('</div>');
                    });//each oFeed.entries
                    sHtml = aHtml.join("");
                    $("#" + id).append('<ul class="feedEkList">' + sHtml + "</ul>");
                    if (callGetTitle) {
                        callGetTitle(oFeed.title);
                    }
                } else {
                    var sInfo = data.responseDetails;
                    alert('Error:'+sInfo);
                }
                //success event:
                $(".itemOne").click(function () {
                    //$(".itemOne .iShowInline").removeClass("iShowInline").addClass("iHide");
                    var oJqImg = $(this).find("img");
                    var sImgLink;
                    $.each(oJqImg, function (i, oImg) {
                        sImgLink = $(oImg).attr("delayLoad");
                        $(oImg).attr("src", sImgLink);
                    });
                    var oJqTitle = $(this).find("li");
                    if (oJqTitle.hasClass("fontLarger")) {
                        oJqTitle.removeClass("fontLarger");
                        $(this).find(".itemIcon").removeClass("glyphicon-chevron-up").addClass("glyphicon-chevron-down");
                    } else {
                        oJqTitle.addClass("fontLarger");
                        $(this).find(".itemIcon").removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-up");
                    }
                    
                    $(this).find("span").fadeToggle("fast");
                    $(this).find(".itemImg").slideToggle("fast");

                });
                $(".itemOne").mouseenter(function () {
                    $(this).addClass("itemOneBorder");
                    //$(this).find("itemIcon").removeClass("glyphicon-chevron-up");
                    $(this).find(".itemIcon").fadeIn("fast");
                });
                $(".itemOne").mouseleave(function () {
                    var jQthat = $(this);                  
                    //setTimeout(function () {
                    //}, 20);
                    if (!(jQthat.find("li").hasClass("fontLarger"))) {
                        jQthat.removeClass("itemOneBorder");
                        jQthat.find(".itemIcon").fadeOut("fast");
                    }
                });
                $(".glyphicon-plus").click(function () {
                    if ((typeof myRss) !== undefined) {     //rss.js
                        myRss.addFeed(oFeed.feedUrl, oFeed.title);
                    }
                });
            }//success
        });//ajax
    };
    var func1 = {
        ConvToLocalTime: function (sDate,sStyle) {
            var i = new Date(sDate);           
            if (sStyle == "date") { return i.toLocaleDateString(); }
            else if (sStyle == "time") { return i.toLocaleTimeString(); }
            else { return i.toLocaleString(); }
        }
    }
})(jQuery);
